package com.ibeeking.found.upms.s.rest.controller.v1;

import com.ibeeking.found.common.annotation.LoginUser;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.found.upms.s.rest.service.IShopPermissionService;
import com.ibeeking.found.upms.s.rest.service.IShopRolePermissionService;
import com.ibeeking.found.upms.s.rest.service.IShopRoleService;
import com.ibeeking.found.upms.s.rest.service.IShopUserRoleService;
import com.ibeeking.found.upms.service.common.bo.ShopPermissionBO;
import com.ibeeking.found.upms.service.common.bo.ShopRolePermissionBO;
import com.ibeeking.found.upms.service.common.bo.ShopUserRoleBO;
import com.ibeeking.found.upms.service.common.query.ShopRolePageQuery;
import com.ibeeking.found.upms.service.common.vo.ShopRolePageVO;
import com.ibeeking.found.upms.service.common.vo.ShopRoleVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.data.Collections3;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName RoleController控制器
 * @Description 租户角色管理
 * @Author ibeeking
 * @Date 2020-12-02 15:29
 **/
@RestController
@RequestMapping("/b/role/v1")
public class ShopRoleController {

    @Resource
    private IShopRoleService shopRoleService;
    @Resource
    private IShopUserRoleService shopUserRoleService;
    @Resource
    private IShopRolePermissionService shopRolePermissionService;
    @Resource
    private IShopPermissionService shopPermissionService;

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "分页查询商户角色列表")
    @PostMapping(value = "/pageList")
    public Page<ShopRolePageVO> pageList(@LoginUser CurrentUser currentUser, @RequestBody ShopRolePageQuery query) {
        return shopRoleService.pageList(currentUser.getCurrentShop().getShopId(), query);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询所有商户角色列表")
    @GetMapping(value = "/allList")
    public List<ShopRoleVO> allList() {
        return shopRoleService.allList();
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询商户角色")
    @GetMapping(value = "/view/{id}")
    public ShopRoleVO view(@PathVariable("id") Long id) {
        return shopRoleService.view(id);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "查询用户id列表")
    @GetMapping(value = "/queryUserIdsByRoleId/{roleId}")
    public List<String> queryUserIdsByRoleId(@LoginUser CurrentUser currentUser, @PathVariable("roleId") @NotNull(message = "roleId不能为空") Long roleId) {
        List<ShopUserRoleBO> shopUserRoleBos = shopUserRoleService.queryUserRoleByRoleIds(currentUser.getCurrentShop().getShopId(), Arrays.asList(roleId));
        if (Collections3.isEmpty(shopUserRoleBos)) {
            return Collections3.emptyList();
        }
        return shopUserRoleBos.parallelStream().map(userRoleBO -> String.valueOf(userRoleBO.getUserId())).distinct().collect(Collectors.toList());
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "根据角色查询权限id列表")
    @GetMapping(value = "/queryPermissionIdsByRoleId/{roleId}")
    public List<String> queryPermissionIdsByRoleId(@LoginUser CurrentUser currentUser, @PathVariable("roleId") @NotNull(message = "roleId不能为空") Long roleId) {
        Long administratorId = shopRoleService.selectAdministratorId();
        if (roleId.equals(administratorId)) {
            //查询当前商户拥有的所有菜单id
            List<ShopPermissionBO> shopPermissionBos = shopPermissionService.selectAllList();
            if (Collections3.isEmpty(shopPermissionBos)) {
                return Collections3.emptyList();
            }
            return shopPermissionBos.parallelStream().map(shopPermissionBO -> String.valueOf(shopPermissionBO.getId())).distinct().collect(Collectors.toList());
        }

        List<ShopRolePermissionBO> rolePermissionBos = shopRolePermissionService.queryRolePermissionByRoleId(roleId);
        return rolePermissionBos.parallelStream().map(rolePermissionBO -> String.valueOf(rolePermissionBO.getPermissionId())).distinct().collect(Collectors.toList());
    }
}
